package com.bw.z.mvp.view.news;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.LogUtils;
import com.bw.z.R;
import com.bw.z.mvp.HttpCode;
import com.bw.z.mvp.base.view.BaseVpFragment;
import com.bw.z.mvp.contract.NewsContract;
import com.bw.z.mvp.di.DaggerNewsComponent;
import com.bw.z.mvp.di.NewsModules;
import com.bw.z.mvp.model.entity.BaseEntity;
import com.bw.z.mvp.model.entity.ImageBannerEntity;
import com.bw.z.mvp.model.entity.NewsEntity;
import com.bw.z.mvp.presenter.NewsPresenter;
import com.bw.z.mvp.view.news.adapter.ContentNewsAdapter;
import com.bw.z.mvp.view.news.adapter.ImageBannerAdapter;
import com.bw.z.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContentNewsFragment extends BaseVpFragment<NewsPresenter>
        implements NewsContract.NewView, OnRefreshLoadMoreListener, OnItemClickListener {

    private long channel_id;
    private SmartRefreshLayout contentNewsSmart;
    private RecyclerView contentNewsRv;
    private View headView;
    private ViewPager headVp;
    private ImageBannerAdapter vpAdapter;
    private ContentNewsAdapter contentNewsAdapter;
    private List<NewsEntity.Values> list = new ArrayList<>();
    private String url;
    private long id;

    public ContentNewsFragment(long channel_id) {
        this.channel_id = channel_id;
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_content_news;
    }

    @Override
    public void initView() {
        //注册广播
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("vpBanner");
        getContext().registerReceiver(new MyBroadcast(), intentFilter);
        headView = LayoutInflater.from(getContext()).inflate(R.layout.view_news_banner, null);
        headVp = headView.findViewById(R.id.news_banner_vp);
        contentNewsSmart = f(R.id.content_news_smart);
        contentNewsSmart.setEnableLoadMore(true);
        contentNewsSmart.setEnableRefresh(true);
        contentNewsSmart.setRefreshHeader(new ClassicsHeader(getContext()));
        contentNewsSmart.setRefreshFooter(new ClassicsFooter(getContext()));
        contentNewsSmart.setOnRefreshLoadMoreListener(this);
        contentNewsRv = f(R.id.content_news_rv);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        contentNewsRv.setLayoutManager(manager);
        contentNewsAdapter = new ContentNewsAdapter();
        contentNewsAdapter.addHeaderView(headView);
        contentNewsRv.setAdapter(contentNewsAdapter);
        contentNewsAdapter.setOnItemClickListener(this::onItemClick);
    }

    @Override
    public void inject() {
        DaggerNewsComponent.builder().newsModules(new NewsModules(this)).build().injectContentNews(this);
    }

    @Override
    public void initData() {
        //根据创建当前fragment对象时的channel_id获取对应新闻数据
        Map<String, Object> map = new HashMap<>();
        map.put("code", HttpCode.CHANNELNEWSCODE);
        map.put("channel_id", channel_id);
        Map<String, Object> bannerMap = new HashMap<>();
        bannerMap.put("code", HttpCode.IMAGERBANNERCODE);
        p.requestAll(bannerMap, map);
    }

    @Override
    public void refreshAll(BaseEntity baseEntity) {
        if (baseEntity instanceof ImageBannerEntity) {
            //banner列表返回
            ImageBannerEntity bannerEntity = (ImageBannerEntity) baseEntity;
            vpAdapter = new ImageBannerAdapter(bannerEntity.getValues());
            headVp.setAdapter(vpAdapter);
        }
        if (baseEntity instanceof NewsEntity) {
            //新闻列表返回
            NewsEntity newsEntity = (NewsEntity) baseEntity;
            contentNewsAdapter.setNewInstance(newsEntity.getValues());
        }
    }

    @Override
    public void refreshRecyclerView(BaseEntity baseEntity) {
        NewsEntity newsEntity = (NewsEntity) baseEntity;
        LogUtils.json(newsEntity);
        contentNewsAdapter.setNewInstance(newsEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity baseEntity) {
        NewsEntity newsEntity = (NewsEntity) baseEntity;
        LogUtils.json(newsEntity);
        if (newsEntity.getValues() == null) {
            ToastUtils.showMsg("最后一页");
            return;
        }
        if (newsEntity.getValues().size() == 0) {
            ToastUtils.showMsg("最后一页");
            return;
        }
        list = contentNewsAdapter.getData();
        list.addAll(newsEntity.getValues());
        contentNewsAdapter.setNewInstance(list);
        contentNewsAdapter.notifyDataSetChanged();
    }


    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        ToastUtils.showMsg("上拉");
        refreshLayout.finishLoadMore();
        Map<String, Object> newsMap = new HashMap<>();
        newsMap.put("code", HttpCode.CHANNELNEWSCODE);
        newsMap.put("channel_id", channel_id);
        p.requestLoad(newsMap);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        ToastUtils.showMsg("下拉");
        if (list != null)
            list.clear();
        refreshLayout.finishRefresh();
        Map<String, Object> newsMap = new HashMap<>();
        newsMap.put("code", HttpCode.CHANNELNEWSCODE);
        newsMap.put("channel_id", channel_id);
        p.requestRefresh(newsMap);
    }


    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        NewsEntity.Values values = (NewsEntity.Values) adapter.getItem(position);
        url = values.news_url;
        id = values.news_id;
        Bundle bundle = new Bundle();
        bundle.putLong("news_id", id);
        bundle.putString("news_url", url);
        LogUtils.e("news_id:" + id);
        startPage(bundle, NewsValuesActivity.class);
    }

    class MyBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("vpBanner")) {
                int type = intent.getIntExtra("type", 1);
                Bundle bundle = new Bundle();
                bundle.putLong("news_id", type);
                bundle.putString("news_url", "http://118.195.161.134:8066/index.html#/");
                startPage(bundle, NewsValuesActivity.class);
            }
        }
    }
}